<?php

/**
 * 关于极光推送的相关接口
 * Class JPushAction
 * @author mh<GALAXY_PHP@163.com> 2018-04-27
 */

class JPushAction extends Action {

	public function sendJPushMessage ($platform,$audience,$audienceType,$alert,$extra) {
	//public function sendJPushMessage () {
//		$platform = 'android';
//		$audience = 'all';
//		$audienceType = 'all';
//		$alert = '测试后台推送信息测试后台推送信息测试后台推送信息测试后台推送信息测试后台推送信息qqqqqqasdwer214234t345vcy6yu89zx4z';
//		$extra =array(
//            'title' => '测试后台222',
//            'extras' => array(
//                'load_url' => '
//http://mall.exiaokang.cn/wx/call.html?userName=&customerId=134&module=product&method=getProduct&params=%7B%22id%22:18487%7D'
//            )
//        );
        $appKey = '83f93653313daa02b6e54caa';
        $masterSecret = 'aeaec0df94122b3cd2b7a862';
        if ($audienceType == 'all'){
        	$response = $this->sendAll($appKey,$masterSecret,$platform,$alert,$extra);
        }else if ($audienceType == 'registrationId'){
        	$response = $this->sendRegistrationId($appKey,$masterSecret,$platform,$audience,$alert,$extra);
        }
        log::write('JPush Extra:'.$alert.json_encode($extra));
        log::write('Jpush:'.json_encode($response));

    }
	private function sendAll($appKey,$masterSecret,$platform,$alert,$extra) {

		$client = new JPushClient($appKey ,$masterSecret,null,'','BJ');
		$base64_auth_string = base64_encode($appKey.':'.$masterSecret);
		$response = $client->push()
        ->setPlatform($platform)
        ->addAllAudience()
        ->androidNotification($alert, $extra)        
        ->options(array(
            'apns_production' => false,
            'priority' => 2
        ))
        ->send($base64_auth_string);
         return $response;
	}

	private function sendRegistrationId ($appKey,$masterSecret,$platform,$audience,$alert,$extra) {

		$client = new JPushClient($appKey ,$masterSecret,null,'','BJ');
		$base64_auth_string = base64_encode($appKey.':'.$masterSecret);
		$response = $client->push()
        ->setPlatform($platform)
        ->setCid($cid)
        ->addRegistrationId($audience)  
        ->androidNotification($alert, $extra)      
        ->options(array(
            'apns_production' => false,
            'priority' => 2
        ))
        ->send($base64_auth_string);
         return $response;
	}
}